package iii.myangel;


import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;

import sql.ReadOldInfo;

import DataClass.OldInfo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class MainControlActivity extends Activity {
	public List<OldInfo> list;
	int oldlistNum;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_maincontrol);
		InitialComponent();
		
		list=ReadOldInfo.list;
		SharedPreferences s1=getSharedPreferences("oldinfo", 0);
		Editor e=s1.edit();
		
		System.out.println("老人資料讀入SP & SPINNER");
		int j=0;
		for( OldInfo oi : list){
			e.putInt("id"+j, oi.id).commit();
			e.putString("name"+j, oi.name).commit();
			e.putString("tel"+j, oi.tel).commit();
			System.out.println(oi.id+"***"+oi.name+"****"+oi.tel);
			j++;
		}
		
		ArrayList<String> oldlist=new ArrayList<String>();
		SharedPreferences s=getSharedPreferences("oldinfo", 0);
		
		for(int i=0;i<list.size();i++){
			//if(!s.getString("id"+i, "").equals(null))
			oldlist.add(s.getString("name"+i, "").toString());
		}
		oldlistNum=oldlist.size();
		
		ArrayAdapter<Object> adapter = new ArrayAdapter<Object>
				(MainControlActivity.this,android.R.layout.
						simple_spinner_item,oldlist.toArray());
		spOld.setAdapter(adapter);
		
	}

	
	OnClickListener btnEatMedicne_click = new OnClickListener() {
		public void onClick(View v) {
			
			GetNameID();
						
			Intent intent = new Intent(MainControlActivity.this,
					EatMedTimeActivity.class);
			startActivity(intent);
			
		}
	};
	OnClickListener btnMapRecord_click = new OnClickListener() {
		public void onClick(View arg0) {

			GetNameID();
			
			Intent intent = new Intent(MainControlActivity.this,
					MapRecordActivity.class);
			startActivity(intent);

		}
	};
	OnClickListener btnEatMedicneRecord_click = new OnClickListener() {
		public void onClick(View arg0) {

			GetNameID();
			
			Intent intent = new Intent(MainControlActivity.this,
					MedicineRecordActivity.class);
			startActivity(intent);

		}
	};
	OnClickListener btnOldManAccount_click = new OnClickListener() {
		public void onClick(View arg0) {

			 GetNameID();
			
			Intent intent = new Intent(MainControlActivity.this,
					PatientActivity.class);
			startActivity(intent);

		}
	};

	

	private void InitialComponent() {
		btnEatMedicne = (Button) findViewById(R.id.btnEatMedicne);
		btnEatMedicne.setOnClickListener(btnEatMedicne_click);
		btnMapRecord = (Button) findViewById(R.id.btnMapRecord);
		btnMapRecord.setOnClickListener(btnMapRecord_click);
		btnEatMedicneRecord = (Button) findViewById(R.id.btnEatMedicneRecord);
		btnEatMedicneRecord.setOnClickListener(btnEatMedicneRecord_click);
		btnOldManAccount = (Button) findViewById(R.id.btnOldManAccount);
		btnOldManAccount.setOnClickListener(btnOldManAccount_click);
		spOld=(Spinner)findViewById(R.id.spUser);
	}
	
	public void GetNameID(){//取老人ID & NAME
		String name="";
		SharedPreferences sp=getSharedPreferences("oldinfo", 0);
		name=spOld.getSelectedItem().toString();
		
		SharedPreferences oldid=getSharedPreferences("olduser", 0);
		Editor e=oldid.edit();
		
		for(int i=0; i<oldlistNum;i++){
			if(name.equals(sp.getString("name"+i, "no"))) {
				e.putInt("id", sp.getInt("id"+i, 0)).commit();
				e.putString("name", name).commit();
			}
		}
		String tmp_print_sp= "";
		{
			
			for(Entry<String, ?> entry : sp.getAll().entrySet()) {
				tmp_print_sp+= "{key: " + entry.getKey() + ", value: " + entry.getValue() + "}\n";
			}
			tmp_print_sp+= "";
		}
		String tmp_print_oldid= "";
		{
			
			for(Entry<String, ?> entry : oldid.getAll().entrySet()) {
				tmp_print_oldid+= "{key: " + entry.getKey() + ", value: " + entry.getValue() + "}\n";
			}
			tmp_print_oldid+= "";
		}
	}

	Button btnEatMedicne = null;
	Button btnMapRecord = null;
	Button btnEatMedicneRecord = null;
	Button btnOldManAccount = null;
	Spinner spOld=null;
}
